package es.cea.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.cea.helpers.EstadisticasHelper;

@WebServlet("/LibroDetail")
public class DetailLibroServlet extends HttpServlet  {

	
	private static final long serialVersionUID = 1L;
    private Connection conexion;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession ses = request.getSession();
		System.out.println("Detalles del libro");
		System.out.println(ses.getAttribute("nombreAdmin"));
		
		EstadisticasHelper.realizarEstadistica(request);
		
		int id = Integer.valueOf(request.getParameter("id"));
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conexion = DriverManager.getConnection ("jdbc:mysql://localhost/libreria","root", "root");
			Statement s = conexion.createStatement();
			ResultSet rs = s.executeQuery ("select libros.id, titulo, libros.fecha, autores.nombre from libros inner join autores on libros.idautor = autores.id where libros.id =" + id);  
			
			rs.next();
			PrintWriter writer = response.getWriter();
			writer.println("<html> <head> " + ses.getAttribute("nombreAdmin") + "<br></head><body>");
			writer.println("<b>DETALLES DE LIBRO</b><br><br>");
			writer.println("Identificador: " + rs.getInt(1) + "<br>");
			writer.println("Nombre libro: " + rs.getString (2)+ "<br>");
			writer.println("Fecha libro: " + rs.getDate (3)+ "<br>");
			writer.println("Nombre autor: " + rs.getString (4)+ "<br><br>");
			writer.println("<a href = 'libros'>Volver al listado de libros</a><br>");
			writer.println("</body> </html>");
			writer.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	
	

}
